import { Button, Result } from "antd";
import { useNavigate } from "react-router-dom";

export interface IRoute {
  path: string;
  absPath: string;
  file?: string;
  id: string;
  parentId?: string;
  [key: string]: any;
}

export const Exception: React.FC<{
  children: React.ReactNode;
  route?: IRoute;
  notFound?: React.ReactNode;
  noAccessible?: React.ReactNode;
  unAccessible?: React.ReactNode;
  noFound?: React.ReactNode;
}> = (props) => {
  const navigate = useNavigate();
  return (
    (!props.route && (props.noFound || props.notFound)) ||
    (props.route?.unaccessible && (props.unAccessible || props.noAccessible)) ||
    ((!props.route || props.route?.unaccessible) && (
      <Result
        status={props.route ? "403" : "404"}
        title={props.route ? "403" : "404"}
        subTitle={props.route ? "抱歉，你无权访问该页面" : "抱歉，你访问的页面不存在"}
        extra={
          <Button type="primary" onClick={() => navigate("/")}>
            返回首页
          </Button>
        }
      />
    )) ||
    props.children
  );
};

export default Exception;
